package com.ruoyi.project.system.notice.service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.notice.domain.Notice;
import com.ruoyi.project.system.notice.domain.NoticeUser;
import com.ruoyi.project.system.notice.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 * @date 2018-06-25
 */
@Service
public class NoticeServiceImpl implements INoticeService
{
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public Notice selectNoticeById(Long noticeId)
    {
        Notice notice = noticeMapper.selectNoticeById(noticeId);
        if (notice != null && "1".equals(notice.getNoticeType())) {
            // 通知
            List<NoticeUser> users = noticeMapper.selectNoticeUsers(noticeId);
            String recipient = users.stream().map(NoticeUser::getRecipient).collect(Collectors.joining(","));
            String recipientName = users.stream().map(NoticeUser::getRecipientName).collect(Collectors.joining(","));
            notice.setRecipient(recipient);
            notice.setRecipientName(recipientName);
        }
        return notice;
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice)
    {
        notice.setRecipient(ShiroUtils.getLoginName());
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNotice(Notice notice)
    {
        notice.setCreateBy(ShiroUtils.getLoginName());
        notice.setCreateTime(DateUtils.getNowDate());
        int n = noticeMapper.insertNotice(notice);
        if ("1".equals(notice.getNoticeType())) {
            // 保存接收人
            String recipient = notice.getRecipient();
            String recipientName = notice.getRecipientName();
            if (StringUtils.isNotNull(recipient)) {
                String[] recipients = recipient.split(",");
                String[] recipientNames = recipientName.split(",");
                List<NoticeUser> list = new ArrayList<>();
                NoticeUser noticeUser = new NoticeUser();
                for (int i = 0; i < recipients.length; i++) {
                    noticeUser = new NoticeUser();
                    noticeUser.setNid(notice.getNoticeId());
                    noticeUser.setRecipient(recipients[i]);
                    noticeUser.setRecipientName(recipientNames[i]);
                    noticeUser.setStatus("0");
                    list.add(noticeUser);
                }
                noticeMapper.insertNoticeUserr(list);
            }
        }
        return n;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNotice(Notice notice)
    {
        notice.setUpdateBy(ShiroUtils.getLoginName());
        int n = noticeMapper.updateNotice(notice);
        // 修改接收人
        if ("1".equals(notice.getNoticeType())) {
            // 先删除再新增
            noticeMapper.deleteNoticeUserByNid(notice.getNoticeId());
            // 保存接收人
            String recipient = notice.getRecipient();
            String recipientName = notice.getRecipientName();
            if (StringUtils.isNotNull(recipient)) {
                String[] recipients = recipient.split(",");
                String[] recipientNames = recipientName.split(",");
                List<NoticeUser> list = new ArrayList<>();
                NoticeUser noticeUser = new NoticeUser();
                for (int i = 0; i < recipients.length; i++) {
                    noticeUser = new NoticeUser();
                    noticeUser.setNid(notice.getNoticeId());
                    noticeUser.setRecipient(recipients[i]);
                    noticeUser.setRecipientName(recipientNames[i]);
                    noticeUser.setStatus("0");
                    list.add(noticeUser);
                }
                noticeMapper.insertNoticeUserr(list);
            }
        }
        return n;
    }

    /**
     * 删除公告对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String ids)
    {
        return noticeMapper.deleteNoticeByIds(Convert.toStrArray(ids));
    }

    // 设置已读
    @Override
    public int readNotice(NoticeUser noticeUser) {
        return noticeMapper.readNotice(noticeUser);
    }

    // 维护接收人
    @Override
    public int insertNoticeUserr(List<NoticeUser> noticeUserList) {
        return noticeMapper.insertNoticeUserr(noticeUserList);
    }

    // 查询未读消息
    @Override
    public Integer selectUnReadNum(String loginName) {
        return noticeMapper.selectUnReadNum(loginName);
    }

    // 批量发送通知
    @Override
    public int insertBatchNotice(Notice notice) {
        notice.setCreateTime(DateUtils.getNowDate());
        int n = noticeMapper.insertNotice(notice);
        if ("1".equals(notice.getNoticeType())) {
            // 保存接收人
            String recipient = notice.getRecipient();
            String recipientName = notice.getRecipientName();
            if (StringUtils.isNotNull(recipient)) {
                String[] recipients = recipient.split(",");
                String[] recipientNames = recipientName.split(",");
                List<NoticeUser> list = new ArrayList<>();
                NoticeUser noticeUser = new NoticeUser();
                for (int i = 0; i < recipients.length; i++) {
                    noticeUser = new NoticeUser();
                    noticeUser.setNid(notice.getNoticeId());
                    noticeUser.setRecipient(recipients[i]);
                    noticeUser.setRecipientName(recipientNames[i]);
                    noticeUser.setStatus("0");
                    list.add(noticeUser);
                }
                noticeMapper.insertNoticeUserr(list);
            }
        }
        return n;
    }
}
